package org.oos.model;

import org.oos.atom.entries.MultimediaEntry;
import org.oos.model.OosImage.ImageSize;
import org.oos.model.OosVideo.VideoSize;

public abstract class OosMedia {

	protected static final String DEFAULT_PATH = "http://11870.com/multimedia/logos/xg_logo.png";
	
	private CharSequence path;
	private CharSequence type;
	
	@SuppressWarnings("unchecked")
	public static <T extends OosMedia> T create(MultimediaEntry entry) {
		Creator<? extends OosMedia> creator = null;
		if (entry.getContent().getType().contains("image")) {
			creator = OosImage.CREATOR;
		} else {
			creator = OosVideo.CREATOR;
		}
		if (creator != null) {
			return (T) creator.create(entry);
		} else {
			throw new IllegalStateException("Cant create a media type");
		}
	}
	
	protected static interface Creator<Type extends OosMedia> {
		public Type create(MultimediaEntry entry);
	}
	
	protected static interface PathFinder<Type extends OosMedia> {
		public CharSequence getPath(Type media);
	}
	
	public abstract CharSequence getPath(MediaSize size);
	
	/**
	 * This enumeration describes the Sized that an image may come in:
	 * 
	 * <table border="1">
	 * <tr>
	 * <td>Small</td>
	 * <td>Medium</td>
	 * <td>Large</td>
	 * <td>Extra Large</td>
	 * </tr>
	 * <tr>
	 * <td>66x66</td>
	 * <td>92x92</td>
	 * <td>320x320</td>
	 * <td>640x640</td>
	 * </tr>
	 * </table>
	 * 
	 * @author pablo.gonzalez@11870.com
	 * 
	 */
	public enum MediaSize {
		SMALL, MEDIUM, LARGE, XLARGE;
		
		public <T> T toSize(T[] sizes) {
			return sizes[this.ordinal()];
		}
		
		public ImageSize toImageSize() {
			return toSize(ImageSize.values());
		}
		
		public VideoSize toVideoSize() {
			return toSize(VideoSize.values());
		}
		
	}

	public CharSequence getPath() {
		if (path != null) {
			return path;
		} else {
			return DEFAULT_PATH;
		}
	}


	protected void setPath(CharSequence path) {
		this.path = path;
	}


	public CharSequence getType() {
		return type;
	}


	protected void setType(CharSequence type) {
		this.type = type;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof OosMedia)) {
			return false;
		} else {
			OosMedia other = (OosMedia)obj;
			return path.equals(other.path) && type.equals(other.type);
		}
	}
	
}